package work.xiaohong.order.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import work.xiaohong.model.Result;
import work.xiaohong.model.StatusCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import work.xiaohong.order.entity.OrderTbl;
import work.xiaohong.order.feign.AccountFeign;
import work.xiaohong.order.service.OrderTblService;
import work.xiaohong.order.to.AccountTbl;

import java.util.Arrays;
import java.util.List;


@Tag(name = "OrderTbl接口文档")
@RestController
@RequestMapping("/order")
@CrossOrigin
public class OrderTblController {

    @Autowired
    private OrderTblService orderTblService;


    @Autowired
    private AccountFeign accountFeign;

    /***
     * 条件分页查询
     * @param orderTbl
     * @param page
     * @param size
     * @return
     */
    @Operation(summary = "条件分页查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@RequestBody(required = false) @Parameter(name = "OrderTbl对象",required = true) OrderTbl orderTbl, @PathVariable  int page, @PathVariable  int size){
        //调用OrderTblService实现分页条件查询OrderTbl
        Page pageInfo = orderTblService.findPage(orderTbl, page, size);
        return new Result<Page>(true,StatusCode.OK.getCode(),"查询成功",pageInfo);
    }

    /***
     * 分页查询
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @Operation(summary = "分页查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用OrderTblService实现分页查询OrderTbl
        Page pageInfo = orderTblService.findPage(page, size);
        return new Result<Page>(true,StatusCode.OK.getCode(),"查询成功",pageInfo);
    }

    /***
     * 条件查询
     * @param orderTbl
     * @return
     */
    @Operation(summary = "条件查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PostMapping(value = "/search" )
    public Result<List<OrderTbl>> findList(@RequestBody(required = false) @Parameter(name = "OrderTbl对象",required = true) OrderTbl orderTbl){
        //调用OrderTblService实现条件查询OrderTbl
        List<OrderTbl> list = orderTblService.findList(orderTbl);
        return new Result<List<OrderTbl>>(true,StatusCode.OK.getCode(),"查询成功",list);
    }

    /***
     * 根据Id删除orderTbl数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID删除OrderTbl",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameter(in = ParameterIn.PATH, name = "id", description = "删除对象ID", required = true)
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用OrderTblService实现根据ID删除
        orderTblService.delete(id);
        return new Result(true,StatusCode.OK.getCode(),"删除成功");
    }

    /***
     * 根据Id批量删除orderTbl数据
     * @param ids
     * @return
     */
    @Operation(summary = "根据Id批量删除OrderTbl",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @DeleteMapping
    public Result deleteByIds(@RequestBody @Parameter(name = "需要删除的OrderTbl对象id集合",required = true) Long[] ids){
        //调用OrderTblService实现根据ID批量删除
        orderTblService.deleteByIds(Arrays.asList(ids));
        return new Result(true,StatusCode.OK.getCode(),"删除成功");
    }


    /***
     * 根据ID修改OrderTbl数据
     * @param orderTbl
     * @return
     */
    @Operation(summary = "根据ID修改OrderTbl",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PutMapping
    public Result updateById(@RequestBody @Parameter(name = "OrderTbl对象",required = true) OrderTbl orderTbl){
        //调用OrderTblService实现修改OrderTbl
        orderTblService.updateById(orderTbl);
        return new Result(true,StatusCode.OK.getCode(),"修改成功");
    }

    /***
     * 新增OrderTbl数据
     * @param orderTbl
     * @return
     */
    @Operation(summary = "添加OrderTbl",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PostMapping
    public Result add(@RequestBody  @Parameter(name = "OrderTbl对象",required = true) OrderTbl orderTbl){
        //调用OrderTblService实现添加OrderTbl
        orderTblService.add(orderTbl);


        AccountTbl accountTbl = accountFeign.findById(1).getData();
        accountTbl.setMoney(accountTbl.getMoney()-orderTbl.getCount()*orderTbl.getMoney());
        accountFeign.updateById(accountTbl);

        return new Result(true,StatusCode.OK.getCode(),"添加成功");
    }

    /***
     * 根据ID查询OrderTbl数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID查询OrderTbl",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameter(in = ParameterIn.PATH, name = "id", description = "查询对象ID", required = true)
    @GetMapping("/{id}")
    public Result<OrderTbl> findById(@PathVariable Integer id){
        //调用OrderTblService实现根据ID查询OrderTbl
        OrderTbl orderTbl = orderTblService.findById(id);
        return new Result<OrderTbl>(true,StatusCode.OK.getCode(),"查询成功",orderTbl);
    }

    /***
     * 查询OrderTbl全部数据
     * @return
     */
    @Operation(summary = "查询所有OrderTbl",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @GetMapping
    public Result<List<OrderTbl>> findAll(){
        //调用OrderTblService实现查询所有OrderTbl
        List<OrderTbl> list = orderTblService.findAll();
        return new Result<List<OrderTbl>>(true, StatusCode.OK.getCode(),"查询成功",list) ;
    }
}
